//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using EntLibContrib.Resource.Configuration;

namespace EntLibContrib.Resource
{
	/// <summary>
	/// Represents a view to navigate the <see cref="ResourceManagerSettings"/> configuration data.
	/// </summary>
	public class ResourceConfigurationView
	{
		#region Fields
		private IConfigurationSource configurationSource;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the name of the default <see cref="ResourceManagerData"/>.
		/// </summary>
		/// <returns>
		/// The name of the default <see cref="ResourceManagerData"/>.
		/// </returns>
		public string DefaultResourceManager
		{
			get
			{
				ResourceManagerSettings configSettings = this.ResourceManagerSettings;
				return configSettings.DefaultResourceManager;
			}
		}

		/// <summary>
		/// Gets the <see cref="ResourceManagerSettings"/> configuration data.
		/// </summary>
		/// <returns>
		/// The <see cref="ResourceManagerSettings"/> configuration data.
		/// </returns>
		public ResourceManagerSettings ResourceManagerSettings
		{
			get { return (ResourceManagerSettings)configurationSource.GetSection(ResourceManagerSettings.SectionName); }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="ResourceConfigurationView"/> with a <see cref="IConfigurationSource"/>.
		/// </summary>
		/// <param name="configurationSource">An <see cref="IConfigurationSource"/> object.</param>
		public ResourceConfigurationView (IConfigurationSource configurationSource)
		{
			this.configurationSource = configurationSource;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Gets the <see cref="ResourceManagerData"/> from configuration for the named <see cref="ResourceManager"/>
		/// </summary>
		/// <param name="resourceManagerName">The name of the <see cref="ResourceManager"/>.</param>
		/// <returns>
		/// A <see cref="ResourceManagerData"/> object.
		/// </returns>
		public ResourceManagerData GetResourceManagerData (string resourceManagerName)
		{
			if (string.IsNullOrEmpty(resourceManagerName))
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.ExceptionResourceManagerNameNullOrEmpty, resourceManagerName));

			ResourceManagerSettings configSettings = this.ResourceManagerSettings;
			if ( !configSettings.ResourceManagers.Contains(resourceManagerName) )
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.UnableToFindResourceManagerInstance, resourceManagerName));
			return configSettings.ResourceManagers.Get(resourceManagerName);
		}

		/// <summary>
		/// Gets the <see cref="ResourceProviderData"/> from configuration for the named <see cref="IResourceProvider"/>
		/// </summary>
		/// <param name="resourceProviderName">The name of the <see cref="IResourceProvider"/>.</param>
		/// <returns>
		/// A <see cref="ResourceProviderData"/> object.
		/// </returns>
		public ResourceProviderData GetResourceProviderData (string resourceProviderName)
		{
			if ( string.IsNullOrEmpty(resourceProviderName) )
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.ExceptionResourceProviderNameNullOrEmpty, resourceProviderName));

			ResourceManagerSettings configSettings = this.ResourceManagerSettings;
			if ( !configSettings.ResourceProviders.Contains(resourceProviderName) )
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.ExceptionProviderNotDefined, resourceProviderName));
			return configSettings.ResourceProviders.Get(resourceProviderName);
		}
		#endregion
	}
}